﻿/*
 * Copyright (c) 2010 Christoc.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

namespace Com.Christoc.HomeController.Admin
{
    using System;
    using System.Web.UI.WebControls;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Security;
    using DotNetNuke.Services.Localization;

    public partial class DeviceTypesList : HomeControllerModuleBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO: add in a confirmation window when clicking on delete in the GridView Rows
            if (!IsPostBack)
                Localization.LocalizeGridView(ref DeviceTypesListGridView, LocalResourceFile);
            if (!IsEditable)
            {
                AddDeviceTypeLinkButton.Visible = false;
            }
        }

        protected void DeviceTypesListDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            DeviceTypesListGridView.DataBind();
        }

        protected void DeviceTypesListDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            //if we need to update the userid and moduleid we can here
            var dvl = (HomeController_DeviceType)e.NewObject;
            dvl.CreatedByUserId = UserId;
            dvl.LastUpdatedByUserId = UserId;
            dvl.LastUpdatedByUserId = UserInfo.UserID;
            dvl.LastUpdated = dvl.DateCreated = DateTime.Now.ToUniversalTime();
        }

        protected void DeviceTypesListDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
        {
            var dt = (HomeController_DeviceType)e.NewObject;
            dt.LastUpdatedByUserId = UserInfo.UserID;
            dt.LastUpdated = DateTime.Now.ToUniversalTime();
        }


        protected void DeviceTypesListDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            //define where requirements here
        }

        protected void AddDeviceTypeLinkButton_Click(object sender, EventArgs e)
        {
            DeviceTypesListFormView.Visible = true;
            DeviceTypesListGridView.DataBind(); 
        }

        protected void DeviceTypesListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow))
            {
                var dt = ((HomeController_DeviceType)(e.Row.DataItem));

                if ((PortalSecurity.IsInRole("Administrators"))
                || (UserController.GetCurrentUserInfo().UserID == dt.CreatedByUserId))
                {
                    e.Row.Cells[0].Enabled = true;
                }
                else
                {
                    e.Row.Cells[0].Text = string.Empty;
                }
            }
        }
        protected void InsertButton_Click(object sender, EventArgs e)
        {
            DeviceTypesListFormView.Visible = false;
            AddDeviceTypeLinkButton.Text = Localization.GetString("UpdateSuccessful", LocalResourceFile);//"Update Successful - Add Another Listing";
            DeviceTypesListGridView.DataBind();
        }

        protected void InsertCancelButton_Click(object sender, EventArgs e)
        {
            DeviceTypesListFormView.Visible = false;
            DeviceTypesListGridView.DataBind();
        }

    }
}